package hutech.ksv.view;

import hutech.ksv.common.KenhSinhVienContants;
import hutech.ksv.entity.NhaTroEntity;
import hutech.ksv.model.NhaTroDataModel;
import hutech.ksv.service.NhaTroService;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("session")
public class NhaTroManageBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	NhaTroService nhatroService;
	
	@Autowired
	UserLoginManagedBean userLoginManagedBean;

	private int maNT;
	private String diaChiNT;
	private float giaThueMotThang;
	private String moTaNT;
	private boolean trangThaiThue;
	private boolean trangThaiDuyetNT;
	private String tenTKCNT;
	private String tenTKQTV;
	private String hinhAnhNT;
	private NhaTroEntity eNhaTro;
	private List<NhaTroEntity> danhSachMaNhaTro;
	private NhaTroDataModel nhaTroListDataModel;

	public NhaTroEntity geteNhaTro() {
		return eNhaTro;
	}

	public void seteNhaTro(NhaTroEntity eNhaTro) {
		this.eNhaTro = eNhaTro;
	}

	public List<NhaTroEntity> getListNhaTro() {
		return listNhaTro;
	}

	public void setListNhaTro(List<NhaTroEntity> listNhaTro) {
		this.listNhaTro = listNhaTro;
	}

	private List<NhaTroEntity> listNhaTro;

	private NhaTroDataModel savingListDataModel;

	public NhaTroDataModel getSavingListDataModel() {
		return savingListDataModel;
	}

	public void setSavingListDataModel(NhaTroDataModel savingListDataModel) {
		this.savingListDataModel = savingListDataModel;
	}

	public List<NhaTroEntity> getSavingList() {
		return savingList;
	}

	public void setSavingList(List<NhaTroEntity> savingList) {
		this.savingList = savingList;
	}

	private List<NhaTroEntity> savingList;

	private float giaThueTK;
	private String diaChiTK;

	public float getGiaThueTK() {
		return giaThueTK;
	}

	public void setGiaThueTK(float giaThueTK) {
		this.giaThueTK = giaThueTK;
	}

	public String getDiaChiTK() {
		return diaChiTK;
	}

	public void setDiaChiTK(String diaChiTK) {
		this.diaChiTK = diaChiTK;
	}

	public int getMaNT() {
		return maNT;
	}

	public void setMaNT(int maNT) {
		this.maNT = maNT;
	}

	public String getDiaChiNT() {
		return diaChiNT;
	}

	public void setDiaChiNT(String diaChiNT) {
		this.diaChiNT = diaChiNT;
	}

	public float getGiaThueMotThang() {
		return giaThueMotThang;
	}

	public void setGiaThueMotThang(float giaThueMotThang) {
		this.giaThueMotThang = giaThueMotThang;
	}

	public String getMoTaNT() {
		return moTaNT;
	}

	public void setMoTaNT(String moTaNT) {
		this.moTaNT = moTaNT;
	}

	public boolean isTrangThaiThue() {
		return trangThaiThue;
	}

	public void setTrangThaiThue(boolean trangThaiThue) {
		this.trangThaiThue = trangThaiThue;
	}

	public boolean isTrangThaiDuyetNT() {
		return trangThaiDuyetNT;
	}

	public void setTrangThaiDuyetNT(boolean trangThaiDuyetNT) {
		this.trangThaiDuyetNT = trangThaiDuyetNT;
	}

	public String getTenTKCNT() {
		return tenTKCNT;
	}

	public void setTenTKCNT(String tenTKCNT) {
		this.tenTKCNT = tenTKCNT;
	}

	public String getTenTKQTV() {
		return tenTKQTV;
	}

	public void setTenTKQTV(String tenTKQTV) {
		this.tenTKQTV = tenTKQTV;
	}

	public String getHinhAnhNT() {
		return hinhAnhNT;
	}

	public void setHinhAnhNT(String hinhAnhNT) {
		this.hinhAnhNT = hinhAnhNT;
	}

	private UploadedFile file;

	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	// tương ứng hàm isPostPack trong .NET
	@PostConstruct
	public void init() {
		// listNhaTro = loadThongTinNT();
		danhSachMaNhaTro = loadThongTinNT();
		List<NhaTroEntity> danhSachNhaTro = nhatroService.loadThongTinNT();
		nhaTroListDataModel = new NhaTroDataModel(danhSachNhaTro);
		userLoginManagedBean.setLoaiNhanXet(KenhSinhVienContants.PHIEU_NHAN_XET_NHA_TRO);
	}

	// tương ứng hàm PageLoad trong .NET
	public void onLoad() {
		danhSachMaNhaTro = loadThongTinNT();
		List<NhaTroEntity> danhSachNhaTro = nhatroService.loadThongTinNT();
		nhaTroListDataModel = new NhaTroDataModel(danhSachNhaTro);
		userLoginManagedBean.setLoaiNhanXet(KenhSinhVienContants.PHIEU_NHAN_XET_NHA_TRO);
		// hinhAnhNT = file.getFileName();
	}

	public void reLoad() {
		maNT = 0;
		diaChiNT = "";
		giaThueMotThang = 0;
		moTaNT = "";
		trangThaiDuyetNT = false;
		tenTKCNT = "";
		tenTKQTV = "";
	}

	public boolean checkAllInput() {
		boolean result;
		if (diaChiNT.equals("") || diaChiNT == null) {
			result = false;
		} else if (String.valueOf(giaThueMotThang).equals("")) {
			result = false;
		} else if (moTaNT.equals("") || moTaNT == null) {
			result = false;
		} else if (tenTKCNT.equals("") || tenTKCNT == null) {
			result = false;
		} else if (tenTKQTV.equals("") || tenTKQTV == null) {
			result = false;
		} else {
			result = true;
		}
		return result;
	}

	public void copyFile(String fileName, InputStream in) {
		try {
			// write the inputStream to a FileOutputStream
			String destination = "C:\\Studying\\HUTECH\\final-project\\workspace\\kenhsinhvien\\src\\main\\webapp\\images";
			OutputStream out = new FileOutputStream(new File(destination
					+ fileName));

			int read = 0;
			byte[] bytes = new byte[1024];

			while ((read = in.read(bytes)) != -1) {
				out.write(bytes, 0, read);
			}

			in.close();
			out.flush();
			out.close();
			System.out.println("New file created!");
		} catch (IOException e) {
			System.out.println(e.getMessage());

		}

	}

	public String insert() {
		hinhAnhNT = "images/" + fileName;
		try {
			if (checkAllInput() == false) {
				diaChiNT = "";
				giaThueMotThang = 0;
				moTaNT = "";
				trangThaiThue = false;
				trangThaiDuyetNT = false;
				tenTKCNT = "";
				tenTKQTV = "";
			} else {
				NhaTroEntity nhatro = new NhaTroEntity();
				nhatro.setDiaChiNT(diaChiNT);
				nhatro.setGiaThueMotThang(giaThueMotThang);
				nhatro.setMoTaNT(moTaNT);
				nhatro.setTrangThaiThue(trangThaiThue);
				nhatro.setTrangThaiDuyetNT(trangThaiDuyetNT);
				nhatro.setTenTKCNT(tenTKCNT);
				nhatro.setTenTKQTV(tenTKQTV);
				nhatro.setHinhAnhNT(hinhAnhNT);
				nhatroService.insertNhaTro(nhatro);
				return "index";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		reLoad();
		return "insertNhaTro";
		// return "testNavigate";
	}
	
	private String fileName;

	public void upload(FileUploadEvent event) {
		FacesMessage msg = new FacesMessage("Success! ", event.getFile()
				.getFileName() + " is uploaded.");
		FacesContext.getCurrentInstance().addMessage(null, msg);
		try {
			copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
			fileName = event.getFile().getFileName();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String update() {
		hinhAnhNT = "images/" + fileName;
		NhaTroEntity nhatro = new NhaTroEntity();
		try {
			if (checkAllInput() == false) {
				maNT = 0;
				diaChiNT = "";
				giaThueMotThang = 0;
				moTaNT = "";
				trangThaiThue = false;
				trangThaiDuyetNT = false;
				tenTKCNT = "";
				tenTKQTV = "";
			} else {
				nhatro.setDiaChiNT(diaChiNT);
				nhatro.setGiaThueMotThang(giaThueMotThang);
				nhatro.setMoTaNT(moTaNT);
				nhatro.isTrangThaiThue();
				nhatro.isTrangThaiDuyetNT();
				nhatro.setTenTKCNT(tenTKCNT);
				nhatro.setTenTKQTV(tenTKQTV);
				nhatro.setHinhAnhNT(hinhAnhNT);
				nhatro.setMaNT(maNT);
				nhatroService.updateNhaTro(nhatro);
				return "index";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "testNavigate";
	}

	public String timKiemNTTheoGia() {
		if (giaThueTK != 0.0) {
			loadDSNTTheoGia();
		}

		/*
		 * if(giaThueTK == 0.0 || diaChiTK == ""){ return ""; }else if
		 * (giaThueTK != 0.0) { savingList = loadDSNTTheoGia();
		 * savingListDataModel = new NhaTrolDataModel( savingList); }else if
		 * (diaChiTK != "") { savingList = loadDSNTTheoDC(); savingListDataModel
		 * = new NhaTrolDataModel( savingList); }
		 */
		return "";
	}

	public String timKiemNTTheoDC() {
		if (diaChiTK != "") {
			loadDSNTTheoDC();
		}
		return "";
	}

	// Load nha tro
	public List<NhaTroEntity> loadThongTinNT() {
		return nhatroService.loadThongTinNT();
	}

	// Load ma nha tro
	public List<NhaTroEntity> loadMaNT() {
		return nhatroService.loadMaNT();
	}

	// Load nha tro theo gia
	public NhaTroDataModel loadDSNTTheoGia() {
		NhaTroEntity nhatro = new NhaTroEntity();
		nhatro.setGiaThueMotThang(giaThueTK);
		List<NhaTroEntity> danhSachNhaTro = nhatroService.loadDSNTTheoGia(nhatro);
		nhaTroListDataModel = new NhaTroDataModel(danhSachNhaTro);
		return nhaTroListDataModel;
	}

	// Load nha tro theo dia chi
	public List<NhaTroEntity> loadDSNTTheoDC() {
		NhaTroEntity nhatro = new NhaTroEntity();
		nhatro.setDiaChiNT(diaChiTK);
		return nhatroService.loadDSNTTheoDC(nhatro);
	}

	// Test show message
	public void showMessage() {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				"Thông báo", "Thành công.");

		RequestContext.getCurrentInstance().showMessageInDialog(message);
	}

	public List<NhaTroEntity> getDanhSachMaNhaTro() {
		return danhSachMaNhaTro;
	}

	public void setDanhSachMaNhaTro(List<NhaTroEntity> danhSachMaNhaTro) {
		this.danhSachMaNhaTro = danhSachMaNhaTro;
	}

	public NhaTroDataModel getNhaTroListDataModel() {
		return nhaTroListDataModel;
	}

	public void setNhaTroListDataModel(NhaTroDataModel nhaTroListDataModel) {
		this.nhaTroListDataModel = nhaTroListDataModel;
	}

	public String edit() {
		NhaTroEntity nhaTroEntity = new NhaTroEntity();
		nhaTroEntity = nhaTroListDataModel.getRowData();
		maNT = nhaTroEntity.getMaNT();
		giaThueMotThang = nhaTroEntity.getGiaThueMotThang();
		moTaNT = nhaTroEntity.getMoTaNT();
		diaChiNT = nhaTroEntity.getDiaChiNT();
		trangThaiDuyetNT = nhaTroEntity.isTrangThaiDuyetNT();
		trangThaiThue = nhaTroEntity.isTrangThaiThue();
		tenTKCNT = nhaTroEntity.getTenTKCNT();
		tenTKQTV = nhaTroEntity.getTenTKQTV();
		hinhAnhNT = nhaTroEntity.getHinhAnhNT();
		return "updateNhaTro";
		// return "testNavigate";
	}

	public String chitiet() {
		NhaTroEntity nhaTroEntity = new NhaTroEntity();
		nhaTroEntity = nhaTroListDataModel.getRowData();
		maNT = nhaTroEntity.getMaNT();
		giaThueMotThang = nhaTroEntity.getGiaThueMotThang();
		moTaNT = nhaTroEntity.getMoTaNT();
		diaChiNT = nhaTroEntity.getDiaChiNT();
		trangThaiDuyetNT = nhaTroEntity.isTrangThaiDuyetNT();
		trangThaiThue = nhaTroEntity.isTrangThaiThue();
		tenTKCNT = nhaTroEntity.getTenTKCNT();
		tenTKQTV = nhaTroEntity.getTenTKQTV();
		hinhAnhNT = nhaTroEntity.getHinhAnhNT();
		userLoginManagedBean.setLoaiNhanXet(KenhSinhVienContants.PHIEU_NHAN_XET_NHA_TRO);
		return "chiTietNhaTro";
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	// FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
	// "Upload success", file.getFileName() + " is uploaded.");
	// FacesContext.getCurrentInstance().addMessage(null, msg);
}
